package com.fenghongzhang.goods;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class MyAdapter extends BaseAdapter {

    private static final String TAG = "MyAdapter";
    private boolean isAll = false;
    private List<Goods> list;
    private Context context;

    private boolean isCheckAll = false;

    public void setCheckAll(boolean checkAll) {
        isCheckAll = checkAll;
    }

    public MyAdapter(List<Goods> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        Goods goods = list.get(position);
        if(goods.getType() == 0){
            return 0;
        }
        return 1;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolderShop viewHolderShop  =null;
        ViewHolderGoods viewHolderGoods = null;
        if(convertView == null){
            if(getItemViewType(position) == 0){
                convertView = LayoutInflater.from(context).inflate(R.layout.title_layout, null);
                viewHolderShop = new ViewHolderShop();
                viewHolderShop.shop_text = convertView.findViewById(R.id.shop_title);
                convertView.setTag(viewHolderShop);
            }else {
                convertView = LayoutInflater.from(context).inflate(R.layout.goods_layout, null);
                viewHolderGoods = new ViewHolderGoods();
                viewHolderGoods.goods_title = convertView.findViewById(R.id.goods_title);
                viewHolderGoods.goods_cb = convertView.findViewById(R.id.goods_cb);
                viewHolderGoods.goods_price = convertView.findViewById(R.id.goods_price);
                viewHolderGoods.goods_add = convertView.findViewById(R.id.goods_add);
                viewHolderGoods.goods_sub = convertView.findViewById(R.id.goods_sub);
                viewHolderGoods.goods_img = convertView.findViewById(R.id.goods_img);
                viewHolderGoods.goods_num = convertView.findViewById(R.id.goods_num);
                convertView.setTag(viewHolderGoods);
            }
        }else{
            if(getItemViewType(position) == 0){
                viewHolderShop = (ViewHolderShop) convertView.getTag();
            }else{
                viewHolderGoods = (ViewHolderGoods) convertView.getTag();
            }
        }

        if(getItemViewType(position) == 0){
            viewHolderShop.shop_text.setText(list.get(position).getShopTitle());
        }else {
            viewHolderGoods.goods_title.setText(list.get(position).getTitle());
            viewHolderGoods.goods_cb.setChecked(list.get(position).getCheck());
            viewHolderGoods.goods_price.setText(list.get(position).getPrice() + "");
            viewHolderGoods.goods_num.setText(list.get(position).getNum() + "");

            if(isCheckAll){
                viewHolderGoods.goods_cb.setChecked(true);
            }else{
                viewHolderGoods.goods_cb.setChecked(false);
            }



            //如果被选中了.就改变数据源中的状态, 然后在页面中,查状态然后删除
            viewHolderGoods.goods_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    /**
                     * 如果所有框被选中,那么全选被选中
                     * 1,判断所有框是否被选中.
                     * 2,让全选选中.
                     */
                    Goods goods = list.get(position);
                    if(isChecked){
                        goods.setCheck(true);
                    }else{
                        goods.setCheck(false);
                    }
                    list.set(position,goods);
                    //选择的总数
                    int a = 0;
                    for (int i = 0; i < list.size(); i++) {
                        Goods good = list.get(i);
                        if(good.getCheck()) {
                            a++;
                        }else{
                            a--;
                        }
                    }
                    Log.i(TAG, "onCheckedChanged: "+a);
                    Log.i(TAG, "onCheckedChanged: "+(list.size()));
                    if(a == list.size()){
                        MainActivity.handler.sendEmptyMessage(110);
                    }else{
                        MainActivity.handler.sendEmptyMessage(120);
                    }

//
                }
            });

            //加法的逻辑
            final ViewHolderGoods finalViewHolderGoods = viewHolderGoods;
            viewHolderGoods.goods_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Toast.makeText(context, "加一个", Toast.LENGTH_SHORT).show();
                    finalViewHolderGoods.goods_num.setText(Integer.parseInt(finalViewHolderGoods.goods_num.getText().toString())+1+"");

                    //当前的金额 * 数量 -每一行的.
                   Double d =  Double.parseDouble(finalViewHolderGoods.goods_price.getText().toString()) * Double.parseDouble(finalViewHolderGoods.goods_num.getText().toString());

                   //每一行的值
                   Shape.map.put(position,d);

                    Message obtain = Message.obtain();
                    //遍历map,算一下总值
                    Double dd = 0.0;
                    Set<Map.Entry<Integer, Double>> entries = Shape.map.entrySet();
                    for (Map.Entry<Integer, Double> map :entries) {
                        Double value = map.getValue();
                        dd += value;
                    }
                    obtain.obj = dd;
                    MainActivity.handler.sendMessage(obtain);
                }
            });

            //减法的逻辑
            viewHolderGoods.goods_sub.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(Integer.parseInt(finalViewHolderGoods.goods_num.getText().toString())-1 <0){
                        Toast.makeText(context, "不能少于0", Toast.LENGTH_SHORT).show();
                    }else{

                    finalViewHolderGoods.goods_num.setText(Integer.parseInt(finalViewHolderGoods.goods_num.getText().toString())-1+"");

                    //当前的金额 * 数量 -每一行的.
                    Double d =  Double.parseDouble(finalViewHolderGoods.goods_price.getText().toString()) * Double.parseDouble(finalViewHolderGoods.goods_num.getText().toString());

                    //每一行的值
                    Shape.map.put(position,d);

                    Message obtain = Message.obtain();
                    //遍历map,算一下总值
                    Double dd = 0.0;
                    Set<Map.Entry<Integer, Double>> entries = Shape.map.entrySet();
                    for (Map.Entry<Integer, Double> map :entries) {
                        Double value = map.getValue();
                        dd += value;
                    }
                    obtain.obj = dd;
                    MainActivity.handler.sendMessage(obtain);
                    }

                }
            });
        }

        return convertView;
    }

    class ViewHolderShop{
        TextView shop_text;

    }

    class ViewHolderGoods{
        TextView goods_title;
        CheckBox goods_cb;
        TextView goods_price;
        TextView goods_add;
        TextView goods_sub;
        ImageView goods_img;
        TextView goods_num;
    }



}
